﻿using Atlass.Framework.AppService.Stock;
using Atlass.Framework.Core.Base;
using Atlass.Framework.Models.Stock;
using Atlass.Framework.ViewModels;
using Atlass.Framework.ViewModels.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Atlass.Framework.Web.Areas.Stock
{

    [Area("stock")]
    public class StockRemarkController : BaseController
    {

        private readonly StockRemarkAppService _stockRemarkApp;
        public StockRemarkController(IServiceProvider service)
        {
            _stockRemarkApp = service.GetRequiredService<StockRemarkAppService>();
        }


        [RequirePermission("stock:remark:view")]
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }


        [RequirePermission("stock:remark:view")]
        [HttpPost]
        public IActionResult GetPageList(DataTableDto dto)
        {
            int stock_no = RequestHelper.GetPostInt("stock_no",0);
            string stockName = RequestHelper.GetPostString("stock_name", "");
            string stock_tag = RequestHelper.GetPostString("stock_tag", "");
            var data = _stockRemarkApp.GetPageList(dto, stock_no,stockName.Trim(), stock_tag.Trim());

            return Json(data);
        }


        [RequirePermission("stock:remark:add,stock:remark:edit")]
        [HttpGet]
        public IActionResult Form(int id)
        {
            ViewBag.Id = id;
            return View();
        }


        [RequirePermission("stock:remark:add,stock:remark:edit")]
        [HttpPost]
        public IActionResult Save(stock_remark dto)
        {
            if (dto.stock_info.IsEmpty())
            {
                return Error("编号不能为空");
            }

            _stockRemarkApp.Save(dto);

            return Success("数据已保存");
        }


        [RequirePermission("stock:remark:add,stock:remark:edit")]
        [HttpGet]
        public IActionResult GetModel(int id)
        {
            var result = new ResultAdaptDto();
            var model = _stockRemarkApp.GetModel(id);
            result.Add("model", model);
            return Json(result);
        }


        [HttpGet]
        [RequirePermission("#")]
        public IActionResult Detail(int id)
        {
            var model = _stockRemarkApp.GetModel(id);
            if (model == null)
            {
                model = new stock_remark();
                model.stock_name = "暂无数据";
                model.stock_tags = "暂无数据";
                model.stock_info = "暂无数据";
            }
            ViewData.Model = model;

            return View();
        }
    }
}
